package com.baidu.sggc.service;

import com.baidu.sggc.data.req.SolutionInfoCreateReq;
import com.baidu.sggc.data.req.SolutionInfoListReq;
import com.baidu.sggc.data.req.SolutionInfoUpdateReq;
import com.baidu.sggc.data.resp.Page;
import com.baidu.sggc.entity.SolutionInfo;
import com.baidu.sggc.entity.UserContext;
import com.baidu.sggc.mapper.SolutionInfoMapper;
import com.baidu.sggc.core.common.utils.InputValidatorUtils;
import com.baidu.sggc.entity.vo.SolutionInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 解决方案表 服务实现类
 * </p>
 *
 * @author xiongziming
 * @since 2025-02-18
 */
@Slf4j
@Service
public class SolutionInfoService {
  private static Logger logger = LoggerFactory.getLogger(SolutionInfo.class);
  @Autowired
  private SolutionInfoMapper solutionInfoMapper;

  public Page<SolutionInfoVo> getSolutionInfoList(SolutionInfoListReq req) {
    Page<SolutionInfoVo> page;
    try {
      PageHelper.startPage(req.getPageNum(), req.getPageSize());
      List<SolutionInfoVo> solutionInfoList = solutionInfoMapper.getSolutionInfoList(req);
      PageInfo<SolutionInfoVo> pageInfo = new PageInfo<>(solutionInfoList);
      page = new Page<>(
              pageInfo.getTotal(),
              pageInfo.getList(),
              pageInfo.isHasNextPage()
      );
    } catch (Exception e) {
      logger.error("Select failed: " + e.getMessage());
      throw new RuntimeException("Select failed: ", e);
    }
    return page;
  }

  public SolutionInfoVo getSolutionInfoById(Long id) {
    SolutionInfoVo solutionInfoVo = null;
    try {
      LambdaQueryWrapper<SolutionInfo> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(SolutionInfo::getId, id)
              .eq(SolutionInfo::getIsDel, 0);
      SolutionInfo solutionInfo = solutionInfoMapper.selectOne(queryWrapper);
      if (solutionInfo != null) {
        solutionInfoVo = new SolutionInfoVo();
        BeanUtils.copyProperties(solutionInfo, solutionInfoVo);
      }
    } catch (Exception e) {
      logger.error("Select failed: " + e.getMessage());
      throw new RuntimeException("Select failed: ", e);
    }
    return solutionInfoVo;
  }

  public boolean updateSolutionInfoById(SolutionInfoUpdateReq req) {
    boolean b;
    try {
      SolutionInfo solutionInfo = new SolutionInfo();
      BeanUtils.copyProperties(req, solutionInfo);
      // 注入用户信息
      String userId = UserContext.getUserId();
      if (userId != null) {
        solutionInfo.setUpdateId(userId);
      }
      solutionInfo.setUpdateTime(LocalDateTime.now());
      b = solutionInfoMapper.updateSolutionInfoById(solutionInfo) > 0;
    } catch (Exception e) {
      logger.error("Update failed: " + e.getMessage());
      throw new RuntimeException("Update failed: ", e);
    }
    return b;
  }

  public Long createSolutionInfo(SolutionInfoCreateReq req) {
    if (!InputValidatorUtils.isValidName(req.getSolutionName())) {
      throw new IllegalArgumentException("名称不合法，只能包含中英文、数字、下划线，不能以下划线或数字开头,不能为空且长度不超过50");
    }
    try {
      SolutionInfo solutionInfo = new SolutionInfo();
      BeanUtils.copyProperties(req, solutionInfo);
      // 注入用户信息
      String userId = UserContext.getUserId();
      String userName = UserContext.getUserName();
      if (userId != null) {
        solutionInfo.setCreateId(userId);
      }
      if (userName != null) {
        solutionInfo.setCreateName(userName);
      }
      solutionInfoMapper.insert(solutionInfo); // 插入并回填主键
      return solutionInfo.getId(); // 返回主键
    } catch (Exception e) {
      logger.error("Create failed: " + e.getMessage(), e);
      throw new RuntimeException("Create failed: ", e);
    }
  }


  public boolean deleteSolutionInfo(SolutionInfo req) {
    boolean b;
    try {
      b = solutionInfoMapper.deleteById(req) > 0;
    } catch (Exception e) {
      logger.error("Delete failed: " + e.getMessage());
      throw new RuntimeException("Delete failed: ", e);
    }
    return b;
  }
}
